<?php
App::uses('AppController', 'Controller');
App::import('Vendor', 'Uploader.Uploader');

/**
 * Providers Controller
 *
 * @property Provider $Provider
 */
class ProvidersController extends AppController {

    public $uses = array('Image', 'Provider');
    // public $components = array('Img');

    public function beforeFilter() {
        $this->Uploader = new Uploader(array(
            'ajaxField' => 'qqfile'
        ));

    }

/**
 * admin_index method
 *
 * @return void
 */
    public function admin_index() {
        // $title_for_layout = 'Add provider';
        // $this->set(compact('title_for_layout'));
        $this->paginate = array(
        'Provider' => array(
            'limit' => 8,
            //'conditions' => array('Category.category_type' => 2),
            // 'order' => array('SetOfMovie.sm_id' => 'DESC'),
            //'group' => array('week', 'home_team_id', 'away_team_id')
        )
        );
        $providers = $this->paginate('Provider');
        $this->set(compact('providers'));
    }

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function admin_view($id = null) {
        if (!$this->Provider->exists($id)) {
            throw new NotFoundException(__('Invalid provider'));
        }
        $options = array('conditions' => array('Provider.' . $this->Provider->primaryKey => $id));
        $this->set('provider', $this->Provider->find('first', $options));
    }

/**
 * admin_add method
 *
 * @return void
 */
    public function admin_add() {
        $this->Uploader->uploadDir = '/img/providers/';
        if ($this->request->is('post')) {
            $dataorg = $this->request->data;
            if ($data = $this->Uploader->upload('file', array('name' => md5($dataorg['Provider']['name']), 'overwrite' => true))) {
                $path = $this->Uploader->resize(array('width' => 101, 'height' => 62, 'expand' => true, 'aspect' => false));
                $dataimg['name'] = $data['custom_name'].'-resized-' . '101' . 'x' . '62';
                $dataimg['link'] = $path;
                $this->Uploader->delete($data['path']);
                $this->Image->create();
                if ($this->Image->save($dataimg)) {
                    $last_id = $this->Image->getInsertId();
                } else {
                    $this->Session->setFlash(__('The image could not be saved. Please, try again.'));
                }
            }
            $dataprovider['name'] = $dataorg['Provider']['name'];
            $dataprovider['link'] = $dataorg['Provider']['link'];
            $dataprovider['image_id'] = $last_id;

            $this->Provider->create();
            if ($this->Provider->save($dataprovider)) {
                $this->Session->setFlash(__('The provider has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The provider could not be saved. Please, try again.'));
            }
        }
    }

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function admin_edit($id = null) {
        if (!$this->Provider->exists($id)) {
            throw new NotFoundException(__('Invalid provider'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            // debug($this->request->data);die;
            if ($this->Provider->save($this->request->data)) {
                $this->Session->setFlash(__('The provider has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The provider could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('Provider.' . $this->Provider->primaryKey => $id));
            $this->request->data = $this->Provider->find('first', $options);
        }
        // $images = $this->Provider->Image->find('list');
        // $this->set(compact('images'));
    }

    public function admin_change($id = null) {
        //debug($id);die;
        $this->Uploader->uploadDir = '/img/providers/';
        $this->render('admin_edit');
        if (!$this->Image->exists($id)) {
            throw new NotFoundException(__('Invalid image'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            $dataorg = $this->request->data;
              // debug($this->request->data);die;
            if ($data = $this->Uploader->upload('file', array('name' => md5($dataorg['Image']['file']['name']), 'overwrite' => true))) {
                $path = $this->Uploader->resize(array('width' => 101, 'height' => 62, 'expand' => true, 'aspect' => false));
                $dataimg['name'] = $data['custom_name'].'-resized-' . '101' . 'x' . '62';
                $dataimg['link'] = $path;
                $this->Uploader->delete($data['path']);
                $this->Image->id=$id;
                $dataold = $this->Image->findById($id);
                $pathold = $dataold['Image']['link'];

                if ($this->Image->save($dataimg)) {
                    $this->Uploader->delete($pathold);
                     $this->redirect(array('action' => 'index'));
                    // debug($dataimg);die;
                } else {
                    $this->Session->setFlash(__('The image could not be saved. Please, try again.'));
                }
            }
        }
    }

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
    public function admin_delete($id = null) {
        $this->Provider->id = $id;

        $dataold = $this->Provider->findById($id);
        $pathold = $dataold['Image']['link'];
        $idimg = $dataold['Image']['id'];
        //var_dump($idimg);die;
        if (!$this->Provider->exists()) {
            throw new NotFoundException(__('Invalid provider'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Provider->delete()) {
            $this->Image->id = $idimg;
             if ($this->Image->delete()) {
            $this->Uploader->delete($pathold);
             }
            $this->Session->setFlash(__('Provider deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Provider was not deleted'));
        $this->redirect(array('action' => 'index'));
    }
}
